package com.smarthealthring.mm.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.google.gson.Gson;
import com.jakewharton.rxbinding2.view.RxView;
import com.smarthealthring.mm.R;
import com.smarthealthring.mm.base.BaseActivity;
import com.smarthealthring.mm.base.IPresenter;
import com.smarthealthring.mm.bean.StepDetailRes;
import com.smarthealthring.mm.contract.HealthyContract;
import com.smarthealthring.mm.contract.StepDetailContract;
import com.smarthealthring.mm.presenter.StepDetailPresenter;
import com.smarthealthring.mm.utils.ComToolBar;
import com.smarthealthring.mm.utils.RandomUtil;
import com.smarthealthring.mm.utils.SPUtils;
import com.smarthealthring.mm.widgets.WrapContentListView;
import com.zhy.adapter.abslistview.CommonAdapter;
import com.zhy.adapter.abslistview.ViewHolder;

import java.util.HashMap;
import java.util.List;

import butterknife.BindView;

/**
 * 步数二级页
 */
public class StepDetailActivity extends BaseActivity<StepDetailPresenter> implements StepDetailContract.View {

    @BindView(R.id.ll_head)
    LinearLayout mllHeader;
    @BindView(R.id.lv_steps)
    WrapContentListView mLvSteps;
    @BindView(R.id.iv_right)
    ImageView mIvRight;

    @Override
    protected void initComToolBar() {
        ImageView ivLeft = ComToolBar.setleftImg(mContext, R.drawable.back_white);
        RxView.clicks(ivLeft)
                .subscribe(o -> finish());
        ComToolBar.setTitle(mContext, "每日步数");
        ImageView ivRight = ComToolBar.setRightImg(mContext, R.drawable.data_more_white);
        RxView.clicks(ivRight)
                .subscribe(o -> {
                    startActivity(new Intent(this, StepWebActivity.class));
                });

    }

    @Override
    protected int getLayout() {
        return R.layout.act_steps_detail;
    }

    @Override
    protected void initEventAndData(@Nullable Bundle savedInstanceState) {
        mIvRight.setVisibility(View.VISIBLE);

        mPresenter.stepDetail(new HashMap<String, String>() {{
            put("trans_id", "2017");
            put("sign", "1111");
            put("username", SPUtils.getString(SPUtils.USERNAME, ""));
            put("nonce_str", RandomUtil.getRandomString(32));
            put("token", SPUtils.getString(SPUtils.TOKEN, "token=="));
        }});
    }

    @Override
    protected StepDetailPresenter createPresenter() {
        return new StepDetailPresenter();
    }

    @Override
    public void getStepDetailSuccess(StepDetailRes res) {
        if ("1".equals(res.getHasdata())) {
            List<StepDetailRes.WalkmngBean> walkmng = res.getWalkmng();
            mLvSteps.setAdapter(new CommonAdapter<StepDetailRes.WalkmngBean>(this, R.layout.item_data_detail, walkmng) {

                @Override
                protected void convert(ViewHolder viewHolder, StepDetailRes.WalkmngBean item, int position) {
                    viewHolder.setText(R.id.tv_time, walkmng.get(position).getWday());
                    viewHolder.setText(R.id.tv_value, walkmng.get(position).getWalkcount() + "步");

                }

            });
        }

    }

    @Override
    public void getStepDetailFail() {

    }
}
